package town;

import game.Game;
import game.GameDriver;
import game.ResourceBasket;
import game.Round;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.util.prefs.BackingStoreException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class BattleCenter extends TownBuilding{
	
	JButton attack;
	JButton defend;

	public BattleCenter(Game currentGame, GameDriver driver, Round round) {
		super(currentGame, driver, round);
		this.setLayout(new BorderLayout());

		attack = new JButton("Build ATTACK! Mule");
		attack.addActionListener(new ButtonListener(driver,1));
		defend = new JButton("Build SHIELD! Mule");
		defend.addActionListener(new ButtonListener(driver,2));

		add(attack,BorderLayout.WEST);
		add(defend,BorderLayout.EAST);
		add(backButton,BorderLayout.SOUTH);
		
	}

	class ButtonListener implements ActionListener {

		int choice;
		
		public ButtonListener(GameDriver driver, int choice) {
			this.choice = choice;
		}

		public void actionPerformed(ActionEvent e) {
			currentGame.setPhase(5);
			if (choice == 1){
				boolean canBuy = round.getCurrPlayer().getResourceBasket().subtractResource(4, 300);
				if (canBuy){
					round.getCurrPlayer().setAttack();
					round.updateInfoPanel();
					Toolkit toolkit = Toolkit.getDefaultToolkit();
					ImageIcon icon = new ImageIcon(this.getClass().getResource("/images/attack.png"));
					Image image = icon.getImage();
					Cursor c = toolkit.createCustomCursor(image, new Point(0,0), "Attack");
					round.getMapPanel().setCursor(c);
					driver.toPanel(round.getMapPanel(), BorderLayout.CENTER);
				}
				else {
					Component frame = null;
					JOptionPane.showMessageDialog(frame, "You do not have enough MONEY!");
				}
			}
			else {
				boolean canBuy = round.getCurrPlayer().getResourceBasket().subtractResource(4, 300);
				if (canBuy){
					round.getCurrPlayer().setDefend();
					round.updateInfoPanel();
					Toolkit toolkit = Toolkit.getDefaultToolkit();
					ImageIcon icon = new ImageIcon(this.getClass().getResource("/images/defend.png"));
					Image image = icon.getImage();
					Cursor c = toolkit.createCustomCursor(image, new Point(0,0), "Defend");
					round.getMapPanel().setCursor(c);
					driver.toPanel(round.getMapPanel(), BorderLayout.CENTER);
				}
				else {
					Component frame = null;
					JOptionPane.showMessageDialog(frame, "You do not have enough MONEY!");
				}
			}
		}
	}
	
}